package com.itheima.tlias.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias.common.result.Result;
import com.itheima.tlias.constant.EmpJobEnum;
import com.itheima.tlias.mapper.EmpMapper;
import com.itheima.tlias.mapper.StudentMapper;
import com.itheima.tlias.pojo.dto.EmpGenderDataDTO;
import com.itheima.tlias.pojo.dto.EmpJobDataDTO;
import com.itheima.tlias.pojo.dto.StudentCountDataDTO;

import com.itheima.tlias.pojo.vo.EmpGenderDataVO;
import com.itheima.tlias.service.ReportService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class ReportServiceImpl implements ReportService {
    @Resource
    private EmpMapper empMapper;
    @Resource
    private StudentMapper studentMapper;
    private static final int MALE = 1;
    private static final int FEMALE = 2;
    private static final String UNKNOWN_GENDER = "未知";
    private static final String MALE_GENDER = "男性员工";
    private static final String FEMALE_GENDER = "女性员工";

    @Override
    public Result empGenderData() {
        List<EmpGenderDataDTO> list = empMapper.empGenderData();
        List<EmpGenderDataVO> genderDataVOS = list.stream().map(t -> {
            EmpGenderDataVO vo = new EmpGenderDataVO();
            String genderStr = UNKNOWN_GENDER;
            if (t.getGender() == MALE) {
                genderStr = MALE_GENDER;
            } else if (t.getGender() == FEMALE) {
                genderStr = FEMALE_GENDER;
            }
            vo.setName(genderStr);
            vo.setValue(String.valueOf(t.getCount()));
            return vo;
        }).toList();
        return Result.success(genderDataVOS);
    }

    @Override
    public Result empJobData() {
        List<EmpJobDataDTO> list = empMapper.empJobData();
        if (Objects.isNull(list)) {
            return Result.error("数据为空");
        }
        List<String> jobList = list.stream().map(t -> EmpJobEnum.findJobName(t.getJob())).toList();
        List<Integer> dataList = list.stream().map(EmpJobDataDTO::getCount).toList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("jobList", jobList);
        jsonObject.put("dataList", dataList);

        return Result.success(jsonObject);
    }

    @Override
    public Result studentDegreeData() {
        List<Map<String, Integer>> list = studentMapper.studentDegreeData();
        List<Map<String, String>> listVO = list.stream().map(t -> {
            String name = switch (t.get("name")) {
                case 1 -> "初中";
                case 2 -> "高中";
                case 3 -> "大专";
                case 4 -> "本科";
                case 5 -> "硕士";
                case 6 -> "博士";
                default -> "其他";
            };
            Map<String, String> map = new HashMap<>(2);
            map.put("name", name);
            map.put("value", String.valueOf(t.get("value")));
            return map;
        }).toList();
        return Result.success(listVO);
    }

    @Override
    public Result studentCountData() {
        List<StudentCountDataDTO> list = studentMapper.studentCountData();
        if (Objects.isNull(list)) {
            return Result.success();
        }
        List<String> clazzList = list.stream().map(StudentCountDataDTO::getName).toList();
        List<Integer> dataList = list.stream().map(StudentCountDataDTO::getCount).toList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("clazzList", clazzList);
        jsonObject.put("dataList", dataList);
        return Result.success(jsonObject);
    }
}
